/* Copyright (c) 2007-2011 SmartVault Corporation, all rights reserved.
 * 
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
// SampleContext.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "TestToolbarDlg.h"

BEGIN_INTERFACE_MAP(TestToolbarDlg, CCmdTarget)
    INTERFACE_PART(TestToolbarDlg, IID_IContextStateMonitor, StateMonitor)
END_INTERFACE_MAP()

STDMETHODIMP_(ULONG) TestToolbarDlg::XStateMonitor::AddRef()
{
    METHOD_PROLOGUE(TestToolbarDlg, StateMonitor);
    
    return pThis->ExternalAddRef();
}

STDMETHODIMP_(ULONG) TestToolbarDlg::XStateMonitor::Release()
{
    METHOD_PROLOGUE(TestToolbarDlg, StateMonitor)

    return pThis->ExternalRelease();
}

STDMETHODIMP TestToolbarDlg::XStateMonitor::QueryInterface(REFIID iid, LPVOID far* ppvObj)     
{
    METHOD_PROLOGUE(TestToolbarDlg, StateMonitor)

    return pThis->ExternalQueryInterface(&iid, ppvObj);
}
STDMETHODIMP TestToolbarDlg::XStateMonitor::StateChanged (
        struct IContextState * state )
{
    METHOD_PROLOGUE(TestToolbarDlg, StateMonitor)

    long ulCount = 0;
    CString tmpText;
    VARIANT_BOOL bEnable;

    CComPtr<IContextDocumentList> docList;
    state->GetDocuments(&docList);

    //Enable/disable Attach button...
    bEnable = FALSE;
    if(docList != NULL)
        docList->get_CanAttach(&bEnable);
    pThis->m_btnAttach.EnableWindow(bEnable);
    
    //Enable/disable View Documents button...
    state->get_CanViewDocuments(&bEnable);
    if(bEnable && docList != NULL)
        docList->get_Count(&ulCount);

    tmpText.Format(IDS_FORMATVIEWBTN, ulCount);
    pThis->m_btnView.EnableWindow(bEnable);
    pThis->m_btnView.SetWindowText(tmpText);

    //Enable/disable Attach button...
    state->get_CanViewNote(&bEnable);
    pThis->m_btnNotes.EnableWindow(bEnable);

    return S_OK;
}
